package com.zhongwang.business.module.baseCostManage.service;

import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.dao.InputCostsDao;
import com.zhongwang.business.module.baseCostManage.model.InputCostsModel;
import com.zhongwang.business.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 *  服务层
 * @author China.kxx
 * @date 2020/09/18 20:13
 */
@Service
public class InputCostsService {
    @Autowired
    private InputCostsDao inputCostsDao;
    /**
     * 查询
     * @param inputCostsModel
     * @return 工时列表数据
     */
    public List<InputCostsModel> inputCuttingTime (InputCostsModel inputCostsModel) {

        List<InputCostsModel> cutting = inputCostsDao.selectcuttingTime(inputCostsModel);
        return cutting;

    }
    /**
     * 查询
     * @param inputMaterialModel
     * @return 辅材列表数据
     */
    public List<InputCostsModel> inputCuttingMaterial (InputCostsModel inputMaterialModel) {

        List<InputCostsModel> Material = inputCostsDao.selectcuttingMaterial(inputMaterialModel);
        return Material;

    }
    /**
     * 保存人工、辅材信息
     * @param manpower
     * @return
     */
    public int saveManpowerInfo(InputCostsModel manpower) {
        User user = ShiroUtils.getUser();
        if (manpower.getMName() == 1) {
            if (manpower.getManId() == null){
                manpower.setManInputUser(user.getName());
                manpower.setManEditDate(new Date());
                manpower.setFkId(manpower.getKbId());
                manpower.setFkCode(manpower.getCode());
                manpower.setFkInputId(manpower.getInputId());
                manpower.setManDeleteFlag(0);
                manpower.setManReceiveFlag(0);
                inputCostsDao.insertManpower(manpower);
            } else {
                inputCostsDao.updateManpower(manpower);
            }
            return manpower.getMName();
        } else {
            if (manpower.getMid() == null) {
                manpower.setMatInputUser(user.getName());
                manpower.setMatEditDate(new Date());
                manpower.setMfkId(manpower.getKbId());
                manpower.setFkCode(manpower.getCode());
                manpower.setMfkInputId(manpower.getInputId());
                manpower.setMatDeleteFlag(0);
                manpower.setMatReceiveFlag(0);
                inputCostsDao.insertMaterial(manpower);
            } else {
                inputCostsDao.updateMaterial(manpower);
            }
            return manpower.getMName();
        }
    }
    /**
     * 查询一条工时数据的方法
     * @param manId
     * @return
     */
    public InputCostsModel selectOneManpowerTime(Integer manId){
        return inputCostsDao.selectOneManpowerTime(manId);
    }
    /**
     * 查询一条辅材数据的方法
     * @param mid
     * @return
     */
    public InputCostsModel selectOneMaterial(Integer mid){
        return inputCostsDao.selectOneMaterial(mid);
    }



    /**
     * 删除一条工时数据验证方法
     * @param manId
     * @return InputCostsModel
     */
    public int delManHour(Integer manId) {
        List<InputCostsModel> inputCostsModels = inputCostsDao.selectCost(manId);
        if (inputCostsModels.size() > 0){
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 删除一条辅材数据验证方法
     * @param mid
     * @return InputCostsModel
     */
    public int delMaterial(Integer mid) {
        List<InputCostsModel> inputCostsModels = inputCostsDao.selectMaterial(mid);
        if (inputCostsModels.size() > 0){
            return 1;
        } else {
            return 0;
        }
    }
    /**
     * 删除一条工时数据的方法
     * @param manId
     * @return InputCostsModel
     */
    public int deleteManHour(Integer manId) {
        return inputCostsDao.deleteManHour(manId);
    }
    /**
     * 删除一条辅材数据的方法
     * @param mid
     * @return
     */
    public int deleteMaterial(Integer mid) {
        return inputCostsDao.deleteMaterial(mid);
    }
    /**
     * 工时清空（全部）逻辑验证
     * @param inputCostsModel
     * @return InputCostsModel
     */
    public int delManHourAll(InputCostsModel inputCostsModel) {
        List<InputCostsModel> inputCostsModel1 = inputCostsDao.selectManHourAll1(inputCostsModel);
        List<InputCostsModel> inputCostsModel2 = inputCostsDao.selectManHourAll2(inputCostsModel);
        if (inputCostsModel1.size() == 0 && inputCostsModel2.size() > 0){
            return -1;
        }else if (inputCostsModel1.size() == 0 && inputCostsModel2.size() == 0){
            return 0;
        }else if(inputCostsModel1.size() < inputCostsModel2.size()){
            return -1;
        }else if(inputCostsModel1.size() == inputCostsModel2.size()){
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 清空对应工时明细
     * @param inputCostsModel
     * @return InputCostsModel
     */
    public int deleteManHourAll(InputCostsModel inputCostsModel) {
        return inputCostsDao.deleteManHourAll(inputCostsModel);
    }
    /**
     * 辅材清空（全部）逻辑验证
     * @param inputCostsModel
     * @return InputCostsModel
     */
    public int delMatAll(InputCostsModel inputCostsModel) {
        List<InputCostsModel> inputCostsModel1 = inputCostsDao.selectMatAll1(inputCostsModel);
        List<InputCostsModel> inputCostsModel2 = inputCostsDao.selectMatAll2(inputCostsModel);
        if (inputCostsModel1.size() == 0 && inputCostsModel2.size() > 0){
            return -1;
        }else if (inputCostsModel1.size() == 0 && inputCostsModel2.size() == 0){
            return 0;
        }else if(inputCostsModel1.size() < inputCostsModel2.size()){
            return -1;
        }else if(inputCostsModel1.size() == inputCostsModel2.size()){
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 清空对应辅材明细
     * @param inputCostsModel
     * @return InputCostsModel
     */
    public int deleteMatAll(InputCostsModel inputCostsModel) {
        return inputCostsDao.deleteMatAll(inputCostsModel);
    }

    /**
     * 编辑数据验证方法
     * @param manpower
     * @return InputCostsModel
     */
    public int selectVerify(InputCostsModel manpower) {
        List<InputCostsModel> inputCostsModelsMan = inputCostsDao.selectVerifyMan(manpower);
        List<InputCostsModel> inputCostsModelsMat = inputCostsDao.selectVerifyMat(manpower);
        if (manpower.getMName() == 1) {
            if (inputCostsModelsMan.size() > 0) {
                return 1;
            } else {
                return 0;
            }
        } else if (manpower.getMName() == 2) {
            if (inputCostsModelsMat.size() > 0) {
                return 1;
            } else {
                return 0;
            }
        }
        return 2;
    }
}